package com.potenza.training.media.controller;


import com.potenza.training.media.model.CheckChunkResult;
import com.potenza.training.media.model.Result;
import com.potenza.training.media.service.MediaUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/media/upload")
public class MediaUploadController {

    @Autowired
    MediaUploadService mediaUploadService;

    @PostMapping("/register")
    public Result register(@RequestParam("fileMd5") String fileMd5,
                           @RequestParam("fileName") String fileName,
                           @RequestParam("fileSize") Long fileSize,
                           @RequestParam("mimetype") String mimetype,
                           @RequestParam("fileExt") String fileExt) {
        return mediaUploadService.register(fileMd5,fileName,fileSize,mimetype,fileExt);
    }

    @PostMapping("/checkchunk")
    public CheckChunkResult checkchunk(@RequestParam("fileMd5") String fileMd5,
                                       @RequestParam("chunk") Integer chunk,
                                       @RequestParam("chunkSize") Integer chunkSize) {
        return mediaUploadService.checkchunk(fileMd5,chunk,chunkSize);
    }

    @PostMapping("/uploadchunk")
    public Result uploadchunk(@RequestParam("file") MultipartFile file,
                                      @RequestParam("fileMd5") String fileMd5,
                                      @RequestParam("chunk") Integer chunk) throws IOException {
        return mediaUploadService.uploadchunk(file,fileMd5,chunk);
    }

    @PostMapping("/mergechunks")
    public Result mergechunks(@RequestParam("fileMd5") String fileMd5,
                                      @RequestParam("fileName") String fileName, @RequestParam("fileSize") Long fileSize,
                                      @RequestParam("mimetype") String mimetype, @RequestParam("fileExt") String fileExt)
    {
        return mediaUploadService.mergechunks(fileMd5,fileName,fileSize,mimetype,fileExt);
    }
}

